package com.vineetska.contact.adapter;

/**
 * @author vineet shukla
 * @email: vineetska@gmail.com
 * Nov 5, 2011
 */

import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.vineetska.contact.R;

public class ContactsListAdapter extends BaseAdapter{

	private Context mContext;
	private Vector mValuestoShow;
	private Vector<String> mDisabledValues;
	private Vector mDisabledPosition;

	public ContactsListAdapter(Context context, Vector vector){
		mContext = context;
		mValuestoShow = vector;
		mDisabledValues = new Vector<String>();
		mDisabledPosition = new Vector();
	}

	public int getCount() {
		if(null != mValuestoShow){
			return mValuestoShow.size();
		}
		return 0;
	}

	public Object getItem(int position) {
		if(position < mValuestoShow.size())
			return  mValuestoShow.get(position);
		else
			return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	@Override
	public boolean isEnabled(int position) {
		if(!checkChar(getItem(position).toString(), position)){
			return false;
		}
		return super.isEnabled(position);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder ;
		if(convertView == null){
			LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = li.inflate(R.layout.contact_list_items, null);
			holder = new ViewHolder();
			holder.txt_name = (TextView) convertView.findViewById(R.id.name_txt);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		if(!isEnabled(position)){
			holder.txt_name.setBackgroundColor(mContext.getResources().getColor(R.color.disable_row_color));
			holder.txt_name.setTextColor(mContext.getResources().getColor(R.color.white));
		}else{
			holder.txt_name.setBackgroundColor(mContext.getResources().getColor(R.color.background));
			holder.txt_name.setTextColor(mContext.getResources().getColor(R.color.black));
		}
		holder.txt_name.setText(getItem(position).toString());
		return convertView;
	}

	class ViewHolder {
		TextView txt_name;
	}

	private boolean checkChar(String txt, int position){
		boolean ret_val = true;
		for(int i = 0; i < 26; i++){
			try{
				if(String.valueOf(Character.toChars(65 + i)).equals(txt)){
					if(mDisabledValues.contains(txt)){
						if(mDisabledPosition.contains(position)){
							ret_val = false;
						}else{
							ret_val = true;
						}

						break;
					}else{
						mDisabledValues.add(txt);
						mDisabledPosition.add(position);
						ret_val = false;
						break;
					}
//					ret_val = false;
//					break;
				}
			}catch(Exception e){ }
		}

		return ret_val;
	}

}
